﻿package com.mui.controls
{
    import com.gengine.game.*;
    import com.gengine.global.*;
    import com.mui.manager.*;
    import com.mui.skins.*;
    import com.mui.utils.*;
    import fl.controls.*;
    import fl.core.*;
    import flash.events.*;

    public class GButton extends Button implements IToolTipItem, IFrUI, IData
    {
        private var _isDisposed:Boolean;
        private var _isMouseOver:Boolean = false;
        public const CLASSNAME:String = "Button";
        protected var _toolTipData:Object;
        private var _reactionTime:int = 500;
        protected var _styleName:String;
        protected var _isStyleChange:Boolean = false;
        public var isUseFilterSkin:Boolean = true;
        private var _paddingTop:Number = 0;
        private var _textFieldHeight:Number = 0;
        private var _Data:Object;

        public function GButton()
        {
            this.buttonMode = true;
            this.useHandCursor = true;
            this.tabChildren = false;
            this.addEventListener(Event.ADDED_TO_STAGE, this.judgeToolTip);
            this.addEventListener(Event.REMOVED_FROM_STAGE, this.judgeToolTip);
            return;
        }// end function

        protected function judgeToolTip(event:Event = null) : void
        {
            if (event && event.type == Event.ADDED_TO_STAGE && this.toolTipData || !event && this.toolTipData && Global.stage.contains(this))
            {
                ToolTipsManager.register(this);
            }
            else
            {
                ToolTipsManager.unregister(this);
            }
            if (event && event.type == Event.ADDED_TO_STAGE)
            {
                this.configEventListener(MouseEvent.MOUSE_OVER, this.showMouseHand);
                this.configEventListener(MouseEvent.MOUSE_OUT, this.hideMouseHand);
            }
            if (event && event.type == Event.REMOVED_FROM_STAGE && this._isMouseOver)
            {
                this.hideMouseHand();
            }
            return;
        }// end function

        protected function showMouseHand(event:MouseEvent) : void
        {
            if (this.enabled)
            {
                this._isMouseOver = true;
            }
            return;
        }// end function

        protected function hideMouseHand(event:MouseEvent = null) : void
        {
            this._isMouseOver = false;
            return;
        }// end function

        override protected function keyDownHandler(event:KeyboardEvent) : void
        {
            return;
        }// end function

        override protected function keyUpHandler(event:KeyboardEvent) : void
        {
            return;
        }// end function

        public function get toolTipData()
        {
            return this._toolTipData;
        }// end function

        public function set toolTipData(param1) : void
        {
            this._toolTipData = param1;
            this.judgeToolTip();
            return;
        }// end function

        public function get reactionTime() : int
        {
            return this._reactionTime;
        }// end function

        public function set reactionTime(param1:int) : void
        {
            this._reactionTime = param1;
            return;
        }// end function

        public function configEventListener(param1:String, param2:Function, param3:Boolean = false, param4:int = 0, param5:Boolean = false) : void
        {
            ObjEventListerTool.addObjEvent(this, param1, param2, param3);
            addEventListener(param1, param2, param3, param4, param5);
            return;
        }// end function

        public function set isDisposed(param1:Boolean) : void
        {
            this._isDisposed = param1;
            return;
        }// end function

        override public function removeEventListener(param1:String, param2:Function, param3:Boolean = false) : void
        {
            ObjEventListerTool.removeObjEvent(this, param1, param2, param3);
            super.removeEventListener(param1, param2, param3);
            return;
        }// end function

        final override protected function configUI() : void
        {
            super.configUI();
            this.createChildren();
            return;
        }// end function

        public function get styleName() : String
        {
            return this._styleName;
        }// end function

        public function set styleName(param1:String) : void
        {
            if (this._styleName != param1)
            {
                this._styleName = param1;
                invalidate(InvalidationType.STYLES);
                this._isStyleChange = true;
            }
            return;
        }// end function

        final override protected function draw() : void
        {
            if (isInvalid(InvalidationType.STYLES))
            {
                if (this._isStyleChange)
                {
                    SkinManager.setComponentStyle(this, this._styleName);
                    this._isStyleChange = false;
                }
                this.updateStyle();
            }
            if (isInvalid(InvalidationType.DATA))
            {
                this.updateDate();
            }
            if (isInvalid(InvalidationType.SIZE))
            {
                this.updateSize();
            }
            if (isInvalid(InvalidationType.SIZE, InvalidationType.SELECTED, InvalidationType.DATA))
            {
                this.updateDisplayList();
            }
            try
            {
                super.draw();
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        protected function createChildren() : void
        {
            return;
        }// end function

        protected function updateStyle() : void
        {
            return;
        }// end function

        protected function updateSize() : void
        {
            return;
        }// end function

        protected function updateDate() : void
        {
            return;
        }// end function

        protected function updateDisplayList() : void
        {
            return;
        }// end function

        public function set paddingTop(param1:int) : void
        {
            this._paddingTop = param1;
            this.drawLayout();
            return;
        }// end function

        public function set textFieldHeight(param1:int) : void
        {
            this._textFieldHeight = param1;
            this.drawLayout();
            return;
        }// end function

        override protected function drawLayout() : void
        {
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_1:* = Number(getStyleValue("textPadding"));
            var _loc_2:* = icon == null && mode == "center" ? (ButtonLabelPlacement.TOP) : (_labelPlacement);
            textField.height = textField.textHeight + 4;
            var _loc_3:* = textField.textWidth + 4;
            var _loc_4:* = textField.textHeight + 4;
            var _loc_5:* = icon == null ? (0) : (icon.width + _loc_1);
            var _loc_6:* = icon == null ? (0) : (icon.height + _loc_1);
            textField.visible = label.length > 0;
            if (icon != null)
            {
                icon.x = Math.round((width - icon.width) / 2);
                icon.y = Math.round((height - icon.height) / 2);
            }
            if (textField.visible == false)
            {
                textField.width = 0;
                textField.height = 0;
            }
            else if (_loc_2 == ButtonLabelPlacement.BOTTOM || _loc_2 == ButtonLabelPlacement.TOP)
            {
                _loc_7 = Math.max(0, Math.min(_loc_3, width - 2 * _loc_1));
                if (height > _loc_4)
                {
                    _loc_8 = _loc_4;
                }
                else
                {
                    _loc_8 = height;
                }
                var _loc_9:* = _loc_7;
                _loc_3 = _loc_7;
                textField.width = _loc_9;
                if (this._textFieldHeight)
                {
                    var _loc_9:* = this._textFieldHeight;
                    _loc_4 = this._textFieldHeight;
                    textField.height = _loc_9;
                }
                else
                {
                    var _loc_9:* = _loc_8;
                    _loc_4 = _loc_8;
                    textField.height = _loc_9;
                }
                textField.x = Math.round((width - _loc_3) / 2);
                textField.y = Math.round((height - textField.height - _loc_6 - this._paddingTop) / 2 + (_loc_2 == ButtonLabelPlacement.BOTTOM ? (_loc_6) : (0)));
                if (icon != null)
                {
                    icon.y = Math.round(_loc_2 == ButtonLabelPlacement.BOTTOM ? (textField.y - _loc_6) : (textField.y + textField.height + _loc_1));
                }
            }
            else
            {
                _loc_7 = Math.max(0, Math.min(_loc_3, width - _loc_5 - 2 * _loc_1));
                var _loc_9:* = _loc_7;
                _loc_3 = _loc_7;
                textField.width = _loc_9;
                textField.x = Math.round((width - _loc_3 - _loc_5) / 2 + (_loc_2 != ButtonLabelPlacement.LEFT ? (_loc_5) : (0)));
                textField.y = Math.round((height - textField.height - this._paddingTop) / 2);
                if (icon != null)
                {
                    icon.x = Math.round(_loc_2 != ButtonLabelPlacement.LEFT ? (textField.x - _loc_5) : (textField.x + _loc_3 + _loc_1));
                }
            }
            if (background)
            {
                background.width = width;
                background.height = height;
            }
            return;
        }// end function

        public function dispose(param1:Boolean = true) : void
        {
            if (this._isDisposed && param1)
            {
                if (Global.isDebugModle)
                {
                    Alert.show("存在组件二次释放" + this.toString());
                }
                return;
            }
            this._styleName = "";
            this._isStyleChange = false;
            ObjEventListerTool.delObjEvent(this);
            textField.filters = [];
            this._paddingTop = 0;
            this._textFieldHeight = 0;
            this.enabled = true;
            this.mouseEnabled = true;
            this.mouseChildren = false;
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (param1)
            {
                UICompomentPool.disposeUICompoment(this);
            }
            this._reactionTime = 500;
            return;
        }// end function

        public function get Data()
        {
            return this._Data;
        }// end function

        public function set Data(param1) : void
        {
            this._Data = param1;
            return;
        }// end function

    }
}
